/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.transform_t2d;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author yura
 */
public class Logger {
    public static final String SPACE = "txt.transform.space";
    private static Logger instance = new Logger();

    private Map<String, Object> vars = new HashMap<String, Object>();
    private Print print;

    private Logger() {
        if(Boolean.parseBoolean(System.getProperty("vcforms.transform.debug"))){
            print = new StdPrint();
        } else {
            print = new NoPrint();
        }
    }

    void _out(String msg){
        print.out(msg);
    }

    void _err(String msg){
        print.err(msg);
    }

    Object _var(String name){
        return vars.get(name);
    }

    Object _var(String name, Object value){
        return vars.put(name, value);
    }

    boolean _is(String name){
        return vars.get(name) != null;
    }

    Object _rm(String name){
        return vars.remove(name);
    }

    //////////////////////////////////////////////////////
    ///////////////////  Static methods //////////////////
    //////////////////////////////////////////////////////

    public static void out(String msg){
        instance._out(msg);
    }

    public static void err(String msg){
        instance._err(msg);
    }

    public static Object var(String name){
        return instance._var(name);
    }

    public static Object var(String name, Object value){
        return instance._var(name, value);
    }

    public static boolean is(String name){
        return instance._is(name);
    }

    public static Object rm(String name){
        return instance._rm(name);
    }

    public static void reinit(){
        instance = new Logger();
    }

    //////////////////////////////////////////////////////
    ///////////////////  Inline classes //////////////////
    //////////////////////////////////////////////////////

    interface Print {
        void out(String msg);
        void err(String msg);
    }

    class NoPrint implements Print {

        @Override public void out(String msg) {
        }

        @Override public void err(String msg) {
        }

    }

    class StdPrint implements Print {

        @Override public void out(String msg) {
            System.out.println(msg);
            System.out.flush();
        }

        @Override public void err(String msg) {
            System.err.println(msg);
            System.err.flush();
        }
    }

}
